<?php

echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />";

include_once '../model/Modelo.php';
include_once '../db/ProdutoDAO.php';

$nome = $_POST['nome'];
$descricao = $_POST['descricao'];
$preco = $_POST['preco'];
$serial = addslashes($_REQUEST['serial']);

$file_name = $_FILES["file"]["name"];
$arr = explode(".", $file_name);
$allowedExts = array("gif", "jpeg", "jpg", "png");
$extension = end($arr);

if ((($_FILES["file"]["type"] == "image/gif")
        || ($_FILES["file"]["type"] == "image/jpeg")
        || ($_FILES["file"]["type"] == "image/jpg")
        || ($_FILES["file"]["type"] == "image/png")
        || (getimagesize($_FILES["file"]["tmp_name"])))
        //&& ($_FILES["file"]["size"] < 20000)
        && in_array($extension, $allowedExts)) {
    if ($_FILES["file"]["error"] > 0) {
        echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    } else {
        //echo "Upload: " . $_FILES["file"]["name"] . "<br>";
        //echo "Type: " . $_FILES["file"]["type"] . "<br>";
        //echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
        //echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

        $imagem = addslashes(file_get_contents($_FILES["file"]["tmp_name"]));
    }
} else {
    echo "Invalid file";
}

$produtoDAO = new ProdutoDAO();
$produtoDAO->atualizaProduto($serial,$nome,$descricao,$preco,$imagem);
?>